#ifndef _DB_H_
#define _DB_H_

#include "structures.h"
/*
 * Gets from DB the sectors to which the document
 * belongs.
 * Retrieves an t_privileges structure pointer or
 * NULL if no sectors are available.
 */
t_privileges * getSectorsForTitle(char * title);

t_privileges * getOnlySectorsForTitle(char * title);

/*
 * Gets from DB the sectors that a user may access.
 * Retrieves an t_privileges structure pointer or
 * NULL if no sectors are available.
 */
t_privileges * getSectorsForUser(char * user);

/*
 * Retrieves all the documents.
 */
t_documents * getAllDocuments();

/*
 * Retrieves the content of a document given its title.
 * Retrieves "" if the document doesn't exist.
 */
char * getDocumentContent(char * title, char * sector, char * level);

/*
 * Adds a user to the DB.
 * FOR CONFIGURATION USE ONLY.
 * Expects user and password with 8 characters max each.
 * Expects a public generated key.
 */
void addUser(char * user, char * psw, char * key);

/*
 * Adds a sector to the DB.
 * FOR CONFIGURATION USE ONLY.
 * Expects a sectorname(20 chars max.) and the list of
 * levels in a descendant order, being the first one 
 * the less restrictive.
 */
void addSector(char * sectorname, char * levels);

/*
 * Assigns a privilege to a user.
 * FOR CONFIGURATION USE ONLY.
 * Expects a user (8 chars max.), a sectorname (20 chars. max)
 * and a levelname (20 chars max.)
 */
void addPrivilege(char * user, char * sectorname, char * privilege);

/*
 * Deletes the privileges of a user for a sector.
 * FOR CONFIGURATION USE ONLY.
 * Expects a user (8 chars max.) and the sector.
 */
void delPrivilege(char * user, char * sectorname);

int authenticateUser(char * user, char * hash);

int saveDocument(char * title, char * sector, char * level, char * content, int size);

int saveUserDocumentKey(char * user, char * title, char * key);

char * getKeyForUserDocument(char * user, char * title);

t_users * getUsersForSector(char * sector, int privilege);

char * getLevelForDocumentSector(char * title, char * sector);

char * getUserKey(char * user);

char * getPrivilegeUserSector(char * user, char * sector);

int getMaxLevel(char * sector);

int getDocumentSize(char * title);

int setDocumentLevel(char * title, char * sectorname, char * level);

t_privileges * getLevelsForSector(char * sector);

t_users * getPrivAboveLevel(char * sectorname, char * level);

/*
 * Wild inserts on DB.
 * DO NOT USE. FOR INTERNAL DATABASE CONFIGURATION
 */
void lucyInTheSkyWithDiamonds(char * sql);

#endif
